<?php

/**
 * invierno actions.
 *
 * @package    costabravasfr
 * @subpackage invierno
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class inviernoActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
  	sfProjectConfiguration::getActive()->loadHelpers(array('I18N'));
  	$q = Doctrine_Query::create()->from('ViviendasAlquileres v')->where('v.alquilerinvierno = ?', 1);
  	$this->response->addMeta('keywords', __("alquileresInvierno"));
  	$this->getResponse()->setTitle(__("alquileresInvierno"));
  	$this->viviendas = $q->execute();
  }
  
  public function executeShow(sfWebRequest $request)
  {
  	sfProjectConfiguration::getActive()->loadHelpers(array('I18N'));
//  	$this->response->addMeta('keywords', __("alquileresInvierno"));
  	$this->vivienda = $this->getRoute()->getObject();
  	$this->getResponse()->setTitle(ucwords($this->vivienda->getSlugApartCasa())." ".$this->vivienda->getNombre()." | ".sfContext::getInstance()->getI18N()->__('Location', null, 'messages')." | Begur Holidays | Costa Brava");
  	
  	
  	$this->logMessage("Carpeta fotos: ".sfConfig::get('app_fotos_path'), "info");
  	$this->requestForm = new RecepcionEmailsForm();
  	
  	$this->vivienda = $this->getRoute()->getObject();
  	
  	
  	//La marcamos como visitada
  	$this->getUser()->addViviendaToVisited($this->vivienda->id);
  	$this->logMessage("Numero de visitadas: ".$this->getUser()->numberVisited(), "info");
  	$this->logMessage("Favorites: ".$this->getUser()->getFavoritesString(), "info");
  	$this->getResponse()->setTitle(ucwords($this->vivienda->getSlugApartCasa())." ".$this->vivienda->getNombre()." | ".sfContext::getInstance()->getI18N()->__('Location', null, 'messages')." | Begur Holidays | Costa Brava");
  	
  	$this->availability = Doctrine::getTable('AlquileresDisponibilidad')->getViviendasWithAvailability($this->vivienda);
  	$this->prices = Doctrine::getTable('AlquileresPrecios')->getViviendasWithPrice($this->vivienda);
  	$minPricesArr[] = array();
  	foreach ($this->prices as $precio){
  		$minPricesArr[] = $precio->getPrecio_Euros();
  		$this->logMessage("Precio: ".$precio->getPrecio_Euros(), "info");
  	}
  	$this->logMessage("Precio minimo: ".min($minPricesArr), "info");
  	$this->precioMinimo = 0;
  	if(count($this->prices) > 0){
  		$this->precioMinimo = min($minPricesArr);
  	}
  	
  	//Extrae todos los .jpg numericos
  	$this->fotoPaths = sfFinder::type('file')->name('[0-9]*.jpg')->
  	in(sfConfig::get('app_fotos_path').$this->vivienda->getNombre());
  	
  	$this-> numFotos = count($this->fotoPaths);
  	$this->logMessage("Num fotos y fotospath".$this->numFotos." ".sfConfig::get('app_fotos_path').$this->vivienda->getNombre(), "info");
  	
  	if($this->numFotos!=0){
  		//Recorre los extraidos para crear un array con los nombres ordenados
  		foreach ($this->fotoPaths as $file){
  			$parts=explode("/",$file);
  			$filename=$parts[sizeof($parts)-1];
  			$number=explode(".",$filename);
  			$arrFiles[$number[0]] = $filename;
  			$this->logMessage("arrFiles number ".$filename, "info");
  			//Comprobamos si esta en cache este thumb
  			$cacheFoto = sfFinder::type('file')->name('*'.$this->vivienda->getNombre().'*-'.$filename)->
  			in(sfConfig::get('app_fotos_cache'));
  				
  			if(count($cacheFoto)==0){
  				$this->logMessage('No esta la thumb'.$this->vivienda->getNombre().'*-'.$filename, "info");
  				$thumbnail = new sfThumbnail(100, 75);
  				$thumbnail->loadFile(sfConfig::get('app_static_domain_fotos').$this->vivienda->getNombre().'/'.$filename);
  				// Save the thumbnail
  				$thumbnail->save(sfConfig::get('app_fotos_cache').$this->vivienda->getNombre().'100x75-'.$filename);
  				//$this->logMessage('cacheFotyo: -'.sfConfig::get('app_abs_path_cache').$filename, "info");
  			}
  		}
  		$arrFiles[] = ksort($arrFiles, SORT_NUMERIC);
  		array_pop($arrFiles);
  		$this->arrFiles = $arrFiles;
  		$this->photoNumber = count($arrFiles);
  	
  		$this->logMessage("arrFiles number ".$this->photoNumber, "info");
  	
  		//Si hay fotoComments los obtenemos s?lo de las fotos existentes
  		//if($this->vivienda->getFotocomment()=='s'){
  	
  			$q = Doctrine::getTable('FotosComments')->createQuery('f')
  			->whereIn('f.numero', $arrFiles)
  			->andWhere('f.nombre = ? ', array($this->vivienda->getNombre()));
  			$this->fotoComments= $q->execute();
  	
  			//$this->logMessage("Array comments".$this->fotoComments, "info");
  	
  			//$this->fotoComments = $q->fetchArray();
  			foreach ($this->fotoComments as $comment){
  				//$this->logMessage("Commnet: ".$comment->getNumero().'-'.$comment->getNombre().'-'.$comment->getEs_comment(), "info");
  			}
  		}
  		//}
  	
  		/*$rutaComment = $this->parseRefererComment($request);
  		if($rutaComment!=""){
  			$this->getUser()->setFlash('refererComment', $rutaComment);
  		}*/
  	
  		//Si viene de reserva pago exitoso recuperamos la reserva
  		if($request->getParameter('book') && $request->getParameter('Ds_Order') && $request->getParameter('Ds_Response') === "0000"){
  			$this->reserva = Doctrine::getTable('ReservasAlquileres')->findOneBydsorder($request->getParameter('Ds_Order'));
  			if($this->reserva->getId() != $request->getParameter('book') || $this->reserva->getDsresponse() != "0000"){
  				throw new sfException("No reservation");
  			}
  		}
  		$this->forward404Unless($this->vivienda);
  		$this->setLayout('layoutLocation');
  	
  	
  	
  	
  	
  }
}
